package com.zw.pdm.module.file.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.production.FileSheet;
import com.zw.pdm.entity.rep.VerifyProcess;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.file.model.FileSheetModel;
import com.zw.pdm.module.file.service.FileCheckService;
import com.zw.pdm.module.file.service.FileListService;
import com.zw.pdm.module.report.service.ReportCheckService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName CreateFileController.java
 * @Description TODO
 * @createTime 2020年12月12日 15:22:00
 */
@Controller
@RequiresPermissions("all")
@RequestMapping("file/check")
public class FileCheckController extends BaseController {

    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private FileCheckService fileCheckService;
    @Autowired
    private FileListService fileListService;
    @Autowired
    private ReportCheckService reportCheckService;
    /**
     * @Author mayongfeng
     * @Date 8:52 2020/12/14
     * @Description 导航栏跳转存档清单提交页面
     *
     **/
    @GetMapping
    @RequiresPermissions(value = {"file:check:view"}, logical = Logical.OR)
    public String addFile(Model model){
        model.addAttribute("specials",dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        // 查部级审核人，用于修改审核人功能
        model.addAttribute("users", reportCheckService.findVerifier());
        return "file/check/fileCheckList";
    }

    /**
     * @Author mayongfeng
     * @Date 11:02 2020/12/14
     * @Description 加载存档清单待审核列表功能
     *
     **/
    @PostMapping("/list")
    @ResponseBody
    @RequiresPermissions(value = {"file:check:view"}, logical = Logical.OR)
    public JsonResult fileCheckList(FileSheetModel fileSheet){
        startPage();
        List<FileSheetModel> fileList = fileCheckService.getFileCheckList(fileSheet);
        return jsonResult(fileList);
    }

    /** 存档清单审核页面 */
    @GetMapping("/{id}")
    @RequiresPermissions(value = {"file:check:shh"}, logical = Logical.OR)
    public String modifyVerify(@PathVariable("id") Integer sheetId, Model model) {
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet", fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/check/fileCheck";
    }
    /** 审核页面的同意按钮 */
    @ResponseBody
    @PostMapping("/success")
    @RequiresPermissions(value = {"file:check:shh"}, logical = Logical.OR)
    public JsonResult success(Integer id) {
        return jsonResult(fileCheckService.fileVerify(id));
    }

    /** 驳回存档清单 */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions(value = {"file:check:shh"}, logical = Logical.OR)
    public JsonResult reject(FileSheet sheet) {
        return jsonResult(fileCheckService.fileReject(sheet));
    }

    /** 修改审核人 */
    @ResponseBody
    @PostMapping("/FileEdit")
    @RequiresPermissions({"production:check:edit"})
    public JsonResult editVerify(VerifyProcess verifyProcess) {
        return isNotNull(fileCheckService.editVerify(verifyProcess));
    }


    /** 存档清单审核查看页面 */
    @GetMapping("/info/{id}")
    @RequiresPermissions(value = {"file:check:view"}, logical = Logical.OR)
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet", fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/check/filePrint";

    }

}
